#include "HelloWorldScene.h"
#include "GameUtils/ADUConstants.h"
#include "GameHeart/ADUObCar.h"
#include "GameHeart/ADUCoin.h"
#include <stdio.h>

CCScene* HelloWorld::scene() {
	CCScene *scene = CCScene::create();

	HelloWorld *layer = HelloWorld::create();

	scene->addChild(layer);

	return scene;
}

bool HelloWorld::init() {
	if (!CCLayer::init()) {
		return false;
	}
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

	/* Add Road */
	mRoad = ADURoad::CreateRoadByFileName(TMX_ROAD);
	mRoad->setBackground(mRoad->layerNamed("background"));
	mRoad->setMeta(mRoad->layerNamed("Meta"));
	mRoad->getMeta()->setVisible(false);
	mRoad->setSPeed(640);
	ADUMovable::setRoadSpeed(640);
	this->addChild(mRoad, ADUIndexBackGround);

	/* Add car */
	mcar = ADUCar::CreateCarBYFileName("car.png");
	mcar->setPosition(ccp(visibleSize.width / 2, visibleSize.height / 2));
	mcar->setRoad(mRoad);
	this->addChild(mcar, ADUIndexCar);

	/* Initialize obstacle */
	obCarsStartPosY = visibleSize.height * 1.2;
	obCars = new CCArray();

	/* Config */
	this->setKeypadEnabled(true);
	this->setAccelerometerEnabled(true); // Enable using Accelermoter by implement didAccelerate()
	this->setTouchEnabled(true);
//	mRoad->startRun();
	/* Call update function every 1 sec */
	this->schedule(schedule_selector(HelloWorld::update), 1.0f);
	return true;
}

void HelloWorld::didAccelerate(CCAcceleration* pAccelerationValue) {
	mcar->ditermine(pAccelerationValue);
}

void HelloWorld::update(float dt) {
	mRoad->startRun();
	this->manageObCars();
	this->detectCollision();
}

void HelloWorld::detectCollision() {
	CCObject* it;
	CCARRAY_FOREACH(obCars, it) {
		ADUMovable *obstacle = static_cast<ADUMovable*>(it); // obstacle : Chuong ngai vat ^^
		if (mcar->boundingBox().containsPoint(obstacle->getPosition())) {
			switch (obstacle->getType()) {
			case OBSTACLE_TYPE:
				if_collision_car_obstacle();
				break;
			case COIN_TYPE:
				if_collision_car_coin();
				break;
			default:
				break;
			}
		}
	}
}
void HelloWorld::if_collision_car_coin() {
	CCLOG("An xu roi");
}

void HelloWorld::if_collision_car_obstacle() {
	CCLog("Va cham voi chuong ngai vat");
}
void HelloWorld::manageObCars() {
	ADUMovable* lastCar = (ADUMovable*) obCars->lastObject();

	if (lastCar == NULL
			|| lastCar->getPositionY()
					< (obCarsStartPosY - lastCar->getContentSize().height * 1.2)) {
		ADUMovable* obcar = NULL;
		int ran = rand() % 4;
		if (ran) {
			int ob_id = rand() % 9 + 1;
			char fileName[10];
			sprintf(fileName, "ob%d.png", ob_id);
			obcar = ADUObCar::CreateObCarBYFileName(fileName);
			obcar->setType(OBSTACLE_TYPE);
		} else {
			obcar = ADUCoin::CreateCoinBYFileName("coin.png");
			obcar->setType(COIN_TYPE);
		}
		CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
		float posX = rand() % (int) visibleSize.width / 2
				+ visibleSize.width / 4;
		obcar->setPosition(ccp(posX, obCarsStartPosY));
		this->addChild(obcar, ADUIndexObCar);
		obcar->run();
		obCars->addObject(obcar);
		mRoad->addListener(obcar);
		CCLog("ADD new car");
	}

	CCObject* it;
	CCARRAY_FOREACH(obCars, it) {
		ADUMovable *_car = static_cast<ADUMovable*>(it);
		if (_car->getPositionX() < (-1) * _car->getContentSize().height) {
			this->removeChild(_car);
			obCars->removeObject(_car);
			mRoad->removeListener(_car);
		}
	}
}

void HelloWorld::keyBackClicked(void) {
	closeCallback(NULL);
}

void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) {
	mcar->jump();
}

void HelloWorld::closeCallback(CCObject* pSender) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox(
			"You pressed the close button. Windows Store Apps do not implement a close button.",
			"Alert");
#else
	CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
#endif
}
